//
//  LKTabbar.swift
//  新浪微博
//
//  Created by 胡不归是大虾 on 16/9/22.
//  Copyright © 2016年 胡不归是大虾. All rights reserved.
//

import UIKit

class LKTabbar: UITabBar {
    //创建一个闭包添加相应方法
    var closure: (()->())?
    //重写构造方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        //添加按钮
        addSubview(composeBtn)
        composeBtn .addTarget(self, action: #selector(didClickComposeBtn), for: .touchUpInside)
    }
    @objc private func didClickComposeBtn ()
    {
        closure?()
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //重写layoutSubviews,调整布局
    override func layoutSubviews() {
        super.layoutSubviews()
        //设置宽高
        let btnW = self.bounds.width / 5
        let btnH = self.bounds.height
        //设定下标
        var index = 0;
        //遍历subView
        for subView in subviews {
            //判断是否是UITabBarButton
            if subView.isKind(of: NSClassFromString("UITabBarButton")!) {
                //是的话设置frame
                subView.frame = CGRect(x: CGFloat(index ) * btnW, y: 0, width: btnW, height: btnH)
//                index += 1
//                if index == 2 {
//                    index += 1
//                }
                //三目改写
                index += (index ==  1 ? 2 : 1)
            }
        }
        composeBtn.center = CGPoint(x: self.center.x, y: self.bounds.height * 0.5)
    }
//MARK:懒加载按钮
    lazy var composeBtn : UIButton = {
        let btn = UIButton()
        btn.setImage(#imageLiteral(resourceName: "tabbar_compose_icon_add"), for: .normal)
        btn.setBackgroundImage(#imageLiteral(resourceName: "tabbar_compose_button"), for: .normal)
        btn.sizeToFit()
        return btn
    }()
    
}
